﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Navigation;
using System.Windows;
using System.Windows.Controls;

namespace ThreeByte.ActiveDeck.AddIn.Controls.Wizard.Pages
{
    public class WorkflowCompleter
    {

        private NavigationService _navService;
        public bool IsComplete { get; private set; }

        public WorkflowCompleter(Page page) {
            page.Loaded += new RoutedEventHandler(page_Loaded);
            page.Unloaded += new RoutedEventHandler(page_Unloaded);
        }

        void page_Unloaded(object sender, RoutedEventArgs e) {
            if(_navService != null) {
                _navService.Navigating -= _navService_Navigating;
            }
        }

        void page_Loaded(object sender, RoutedEventArgs e) {
            Page page = (Page)sender;
            _navService = page.NavigationService;
            if(_navService != null) {
                _navService.Navigating += new NavigatingCancelEventHandler(_navService_Navigating);
            }
        }

        void _navService_Navigating(object sender, NavigatingCancelEventArgs e) {
            if(!IsComplete) {
                MessageBoxResult result = MessageBox.Show(Message, Caption, MessageBoxButton.OKCancel);
                if(result == MessageBoxResult.OK) {
                    return;// Allow info to be discarded
                }
                //Otherwise, don't navigate away
                e.Cancel = true;
            }
        }

        public void Complete() {
            IsComplete = true;
            if(_navService != null) {
                _navService.Navigating -= _navService_Navigating;
            }
        }

        private string _message = "Your current activity has not been assigned to a slide in your presentation yet.  If you leave now, this activity will be lost.  Are you sure you want to leave?";
        public string Message {
            get {
                return _message;
            }
            set {
                _message = value;
            }
        }

        private string _caption ="Activity Not Saved";
        public string Caption {
            get {
                return _caption;
            }
            set {
                _caption = value;
            }
        }


    }
}
